<?php

namespace App\Http\Controllers;


use App\Models\Extert;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ExtertController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $oExtert=Extert::paginate(5);
        return view('admin.extert-list')->with('oExtert',$oExtert);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.Extert-creat');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $datas = $request->all();
        $rules = [
            'name' => 'required|alpha_dash|unique:experts',
            'department'=>'required',
            'title'=>'required',
            'duties'=>'required',
            'hospital'=>'required',
            'content'=>'required',
            'edu'=>'required'
        ];
        $message = [
            'name.required' => '用户名不能为空',
            'name.alpha_dash' => '用户名必须是字母、数字，短破折号（-）和下划线（_）',
            'name.unique' => '用户名已存在，不能重复',
            'department.required' => '科室不能为空',
            'title.required' => '职称不能为空',
            'duties.required' => '职务不能为空',
            'hospital.required' => '医院不能为空',
            'content.required'=>'简介不能为空',
            'edu.required'=>'接受教育不能为空'
        ];
        $validate = Validator::make($datas, $rules, $message);
        if ($validate->fails()) {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            if($request->hasFile('thumb')){
                $thumb=$request->thumb;
                $filename = uniqid().'.'.$thumb->extension();
                $thumb->move(public_path().'/upload',$filename);
                $datas["thumb"]= '/upload/'.$filename;
            }
            Extert::create($datas);
            return json_encode(['success' => true, 'msg' => '成功提交']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $oExtert=Extert::find($id);
        if ($oExtert == null) {
            abort(404);
        } else {
            return view('admin.Extert-edit')->with('oExtert',$oExtert);
        }

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $oExtert=Extert::find($id);
        $datas = $request->all();
        $rules = [
            'name' => 'required|alpha_dash',
            'department'=>'required',
            'title'=>'required',
            'duties'=>'required',
            'hospital'=>'required',
            'content'=>'required',
            'edu'=>'required'
        ];
        $message = [
            'name.required' => '用户名不能为空',
            'name.alpha_dash' => '用户名必须是字母、数字，短破折号（-）和下划线（_）',
            'department.required' => '科室不能为空',
            'title.required' => '职称不能为空',
            'duties.required' => '职务不能为空',
            'hospital.required' => '医院不能为空',
            'content.required'=>'简介不能为空',
            'edu.required'=>'接受教育不能为空'
        ];
        $validate = Validator::make($datas, $rules, $message);

        if ($validate->fails()) {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            if($request->hasFile('thumb')){
                $thumb=$request->thumb;
                $filename = uniqid().'.'.$thumb->extension();
                $thumb->move(public_path().'/upload',$filename);
                $datas["thumb"]= '/upload/'.$filename;
            }
            $oExtert->name=$datas['name'];
            $oExtert->thumb=$datas['thumb'];
            $oExtert->department=$datas['department'];
            $oExtert->title=$datas['title'];
            $oExtert->duties=$datas['duties'];
            $oExtert->hospital=$datas['hospital'];
            $oExtert->content=$datas['content'];
            $oExtert->edu=$datas['edu'];
            $oExtert->save();
            return json_encode(['success' => true, 'msg' => '修改成功']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Extert $extert)
    {
        //
        $extert->delete();
        return json_encode(['msg'=>'删除成功']);
    }
    public function people(){
        $oExtert=Extert::paginate(9);

        return view('front.people')->with('oExtert',$oExtert);
    }
    public function expert_list($id){
        $extert=Extert::find($id);
        $oExtert=Extert::where('id','!=',$id)->take(3)->get();//在专家列表里找和find找到不一样的3个expert
        return view('front.people_list')
            ->with('oExtert',$oExtert)
            ->with('extert',$extert);
    }
}
